$(function () {
    obj.init();
});

var obj = new function JsFun() {
    return {
        dataGrid: null,
        dom: {
            search: null
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'res/getReferenceSchedule');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_planApply', 'bt_list_addSchedulePlan']);
            this.dom.search = $('#bt_list_search');
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
            //双击
            this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                $this.funObj().modifyApply(id);
            };
            //增加申请
            $('#bt_list_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().planApply();
            });
            //排期申请
            $('#bt_list_planApply').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addPlan();
            });
            //排期计划
            $('#bt_list_addSchedulePlan').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addPlanApply();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //查找
                search: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague')),
                            applySubject: xhExtra.getValue($('#list_search_applySubject')),
                            applyClass: xhExtra.getValue($('#list_search_applyClass')),
                            applyVolume: xhExtra.getValue($('#list_search_applyVolume')),
                            searchType: xhExtra.getValue($('#list_search_type')),
                            app: xhExtra.getValue($('#list_search_app')),
                            plan: xhExtra.getValue($('#list_search_plan')),
                            existId: xhExtra.getValue($('#list_search_existId'))
                        }
                    })
                },
                planApply: function () {
                    window.top.xhTop.showModal({
                        title: '添加教辅申请',
                        content: xhTool.getFrameString('res/addReferenceSchedule', {}, 280),
                        button: '保存',
                        size: 'lg',
                        width:'800',
                        backClose: false
                    });
                },
                modifyApply: function (_id) {
                    var iBookId = $this.dataGrid.getSelectValue('iBookId');
                    if (iBookId) {
                        window.top.xhTop.showError('已存在排期，不可修改');
                        return false;
                    }
                    var button = '';
                    if (modifyPower) {
                        button = '保存';
                    }
                    window.top.xhTop.showModal({
                        title: '修改申请，编号：' + _id,
                        content: xhTool.getFrameString('res/modifyReferSchedule', {id: _id}, 280),
                        button: button,
                        size: 'lg',
                        width:'800',
                        backClose: false
                    });
                },
                addPlan: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var iBookId = $this.dataGrid.getSelectValue('iBookId');
                    var sBookName = $this.dataGrid.getSelectValue('sBookName');
                    if (!iBookId) {
                        window.top.xhTop.showError('不存在书id,不可申请');
                        return false;
                    }

                    window.top.xhTop.showModal({
                        title: '[排期申请]书：' + sBookName,
                        id: 'apply_form',
                        footerHtml:
                        '<button type="button" class="btn btn-primary xh-btn-load" id="apply_plan_confirm">提交</button>' +
                        '<button type="button" class="btn btn-default" data-dismiss="modal" id="bt_modal_close">关闭</button>',
                        content: xhTool.getFrameString('res/addPlanApplyForm', {type:6, id: id, bookId: iBookId, bookName: sBookName}, 300),
                        size: 'md',
                        width:'800',
                        backClose: false
                    });
                    // window.top.xhTop.createNewTab('排期申请：' + sBookName, 'res/addPlanApplyForm?id='+id+'&bookId='+iBookId+'&bookName='+sBookName);
                },
                addPlanApply: function () {
                    var iId = $this.dataGrid.getSelectValue('iId');
                    var iBookId = $this.dataGrid.getSelectValue('iBookId');
                    var sBookName = $this.dataGrid.getSelectValue('sBookName');
                    if (!iId) {
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    //是否第一次添加排期
                    var isFirst = 1;//不存在
                    if (iBookId) {
                        isFirst = 2;//存在
                    }
                    var footStr = '<button type="button" class="btn btn-default" data-dismiss="modal" id="bt_modal_close">关闭</button>';
                    footStr = '<button type="button" class="btn btn-primary xh-btn-load" id="schedule_form_confirm">保存</button>' + footStr;
                    window.top.xhTop.showModal({
                        title: '[排期计划]编号：' + iId + "，书名：" + sBookName,
                        id: 'schedule_form',
                        footerHtml: footStr,
                        content: xhTool.getFrameString('res/applyPlanDetail', {type: 4, bookId: iBookId, bookName: sBookName, scheduleApplyId: iId, existBookId: isFirst}, $(window).height()-80),
                        size: 'lg',
                        backClose: false
                    });
                }
            }
        }
    }
};

